<?php
/**
 * 咨询师入驻申请
 *
 * User: kaikaic
 * Date: 2018/10/30
 * Time: 22:50
 */

namespace app\psy\controller;

use app\exam\service\CityService;
use app\psy\service\ApplyService;
use app\psy\service\ConsultantIndexService;
use app\psy\service\ConsultantService;
use app\psy\service\OrganizationService;
use app\store\service\OrderService;
use controller\BasicAdmin;
use service\ConfigService;
use think\Db;

class Apply extends BasicAdmin {

    protected $table = "PsyApply";

    public function index() {
        $this->title = "入驻申请";
        $query = array();
        $query['name'] = $this->request->get("name");
        $query['status'] = intval($this->request->get("status"));

        $db = Db::name($this->table);
        if ($query['name']) {
            $db->whereLike("name", "%{$query['name']}%");
        }

        return parent::listRecord($db);
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);
        $config = ConfigService::auditingStatus();

        for ($i=0; $i<$size; $i++) {
            $tmp = $data[$i];

            $tmp['status_txt'] = $config[$tmp['status']];

            $data[$i] = $tmp;
        }
    }

    /**
     * 审核
     */
    public function auditing() {
        if ($this->request->isGet()) {
            $id = $this->request->param('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $record = ApplyService::getInstance()->find($id);
            if (!$record) {
                $this->error('参数错误，记录不存在！');
            }
            return view('form', ['title'=>'入驻审核', 'vo'=>$record]);
        }

        if ($this->request->isPost()) {
            $id = intval($this->request->post("id"));
            if (!$id) {
                $this->error('参数错误，请稍候再试！');
            }

            $audit = intval($this->request->post("audit"));
            $record = ApplyService::getInstance()->find($id);
            $status = 3;
            if ($audit == 1) {
                $status = 2;
                $data['account_id'] = $record['account_id'];
                $data['name'] = $record['name'];
                $data['type_ids'] = serialize(array());
                $data['factions_ids'] = serialize(array());
                $data['goodat_ids'] = serialize(array());
                $data['period_ids'] = serialize(array());
                $data['sort'] = ConsultantService::getInstance()->getMaxSort() + 1;
                $data['status'] = 1;
                $data['create_time'] = time();
               ConsultantService::getInstance()->insert($data);

            }

            $params = array();
            $params['status'] = $status;
            $result = ApplyService::getInstance()->update($params, $id);
            if ($result) {
                $this->success('操作成功!', '');
            }else {
                $this->error('操作失败，请重新再试!');
            }
        }
    }

    /**
     * 入驻申请-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误，请重新操作删除!');
        }

        $record = ApplyService::getInstance()->find($id);
        if (!$record) {
            $this->error('记录不存在，请重新操作!');
        }

        // 审核不通过的才可以删除
        if ($record['status'] != 3) {
            $this->error('记录不允许删除!');
        }

        $result = ApplyService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }
}

